<?php

namespace App\Http\Requests\Auth;

use App\Http\Requests\AbstractRequest;
use App\Models\User;

class SmsLoginRequest extends AbstractRequest
{

    public $user;

    /**
     * 验证器规则
     *
     * @return array
     */
    public function rules()
    {
        return [
            'mobile' => 'required|mobile'
        ];
    }

    /**
     * 验证手机号是否存在
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validate()
    {
        $user = User::where($this->only('mobile'))->first();

        if (!$user) {
            // 账号不存在,登录失败
            $this->validationException([
                'mobile' => [trans('auth.failed')],
            ]);
        }

        $this->user = $user;
    }
}
